package handler

import (
	"gitee.com/gopher2011/casdoor/internal/logic/organization"
	"gitee.com/gopher2011/casdoor/internal/svc"
	"gitee.com/gopher2011/casdoor/internal/types"
	"github.com/gin-gonic/gin"
)

func AddOrganization(c *gin.Context) {
	var req types.Request
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{"code": 400, "msg": err.Error(), "data": nil})
		return
	}
	l := organization.NewOrganization(c.Request.Context(), svc.Svc)
	resp, err := l.AddOrganization(req)
	if err != nil {
		c.JSON(200, gin.H{"code": 400, "msg": err.Error(), "data": nil})
	} else {
		c.JSON(200, resp)
	}
}

func DeleteOrganization(c *gin.Context) {
	var req types.Request
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{"code": 400, "msg": err.Error(), "data": nil})
		return
	}
	l := organization.NewOrganization(c.Request.Context(), svc.Svc)
	resp, err := l.DeleteOrganization(req)
	if err != nil {
		c.JSON(200, gin.H{"code": 400, "msg": err.Error(), "data": nil})
	} else {
		c.JSON(200, resp)
	}
}

func GetOrganization(c *gin.Context) {
	var req types.Request
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{"code": 400, "msg": err.Error(), "data": nil})
		return
	}
	l := organization.NewOrganization(c.Request.Context(), svc.Svc)
	resp, err := l.GetOrganization(req)
	if err != nil {
		c.JSON(200, gin.H{"code": 400, "msg": err.Error(), "data": nil})
	} else {
		c.JSON(200, resp)
	}
}

func GetOrganizations(c *gin.Context) {
	var req types.Request
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{"code": 400, "msg": err.Error(), "data": nil})
		return
	}
	l := organization.NewOrganization(c.Request.Context(), svc.Svc)
	resp, err := l.GetOrganizations(req)
	if err != nil {
		c.JSON(200, gin.H{"code": 400, "msg": err.Error(), "data": nil})
	} else {
		c.JSON(200, resp)
	}
}

func UpdateOrganization(c *gin.Context) {
	var req types.Request
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{"code": 400, "msg": err.Error(), "data": nil})
		return
	}
	l := organization.NewOrganization(c.Request.Context(), svc.Svc)
	resp, err := l.UpdateOrganization(req)
	if err != nil {
		c.JSON(200, gin.H{"code": 400, "msg": err.Error(), "data": nil})
	} else {
		c.JSON(200, resp)
	}
}
